package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.modular.system.entity.BtyArchives;
import cn.stylefeng.guns.sys.modular.system.entity.BtyShopTree;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyArchivesMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyShopTreeMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案管理表 服务实现类
 * </p>
 *
 * @author liuy
 * @since
 */
@Service
public class BtyArchivesService extends ServiceImpl<BtyArchivesMapper, BtyArchives> {
    @Resource
    private BtyShopTreeMapper btyShopTreeMapper;

    /**
     * 获取档案管理列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:05 PM
     */

    public Page<Map<String, Object>> list(String condition,String dm,String tel,String month,String status) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, condition,dm,tel,month,status);
    }

    /**
     * 写入三级区域
     */
    public void insertShop() {
        //获取所有的二级区域
        List<String> secondNameList = baseMapper.getSecondShop();

        int num = 16;

        //获取对应二级区域下面的三级区域
        for (String secondName : secondNameList) {

            //获取bty_shop_name表中对应的二级区域
            BtyShopTree secondShop = btyShopTreeMapper.getBySimpleName(secondName);

            //获取二级区域下的三级区域
            List<String> thirdShopList = baseMapper.getNextShop(secondName);
            BtyShopTree shopSave = new BtyShopTree();
            //将获取到的三级区域插入表中
            for (String thirdName : thirdShopList) {
                num = num + 1;
                shopSave.setShopId(Long.valueOf(num));
                shopSave.setPid(secondShop.getShopId());
                //pids
                shopSave.setPids(secondShop.getPids() + "[" + secondShop.getShopId() + "]");
                shopSave.setSimpleName(thirdName);
                shopSave.setFullName(thirdName);
                shopSave.setCreateTime(new Date());
                btyShopTreeMapper.insert(shopSave);
            }

        }
    }

    /**
     * 写入四级区域-门店
     */
    public void  insertMenDian(){
        //获得所有的店
        List<BtyArchives> allShop = baseMapper.getAllShop();
        int num = 65;

        BtyShopTree shopSave = new BtyShopTree();
        for(BtyArchives shop:allShop){
            num = num + 1;
            //获取bty_shop_name表中三级区域中与店对应的父级区域
            BtyShopTree fuShop = btyShopTreeMapper.getThirdShopBySimpleName(shop.getEqy());
            shopSave.setShopId(Long.valueOf(num));
            shopSave.setPid(fuShop.getShopId());
            //pids
            shopSave.setPids(fuShop.getPids() + "[" + fuShop.getShopId() + "]");
            shopSave.setSimpleName(shop.getDm());
            shopSave.setFullName(shop.getDm());
            shopSave.setCreateTime(new Date());
            btyShopTreeMapper.insert(shopSave);
        }

        System.out.println("一共店有"+allShop.size()+"条");
    }


}
